/**
 ****************************************************************************************************
 * @file    main.c
 * @author  FMSH Application Team
 * @brief   Header file of FL Module
 ****************************************************************************************************
 * @attention
 *
 * Copyright (c) [2021] [Fudan Microelectronics]
 * THIS SOFTWARE is licensed under Mulan PSL v2.
 * can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 *
 ****************************************************************************************************
 */

#include "main.h"

#include "Bsp_adc.h"
#include "Compensation.h"
#include "debug.h"
#include "Protocol_CAN.h"
#include "tick.h"
#include "uds_port.h"
#include "user_uds.h"

void debug_cout_handle(void);

CB_HANDLE_STRUCT task_list[] = {
    { Compensation_Callback, 100, ENABLE, ENABLE, 0 },
    { debug_cout_handle, 100, ENABLE, ENABLE, 0 },
    { CANFrameSend_getdata, 100, ENABLE, ENABLE, 0 },
    { canFrameSend_Handle, 10, ENABLE, ENABLE, 0 },
    { uds_1ms_task, 1, ENABLE, ENABLE, 0 },
    { uds_func_poll_task, 10, ENABLE, ENABLE, 0 },
    { NULL, 0, DISABLE, DISABLE, 0 },
};

extern uint32_t Xadc;

// const char dummy_data[13 * 1024] __attribute__((used)) = { 0 }; // 13KB
// const char dummy_data2[1 * 585] __attribute__((used))  = { 0 };

int main(void)
{

#if DEBUG
    SEGGER_RTT_Init();
    SEGGER_RTT_TerminalPrintf(0, "RTT initialized\r\n");
#endif

    BSP_init();
    Compensation_Init();

    /* 开启中断, 因为在BootLoader中禁用了中断，所以需要手动开启 */
    __enable_irq();

    while (1) {
        // LOG("HUEY\n");
        call_back_task_run(task_list);
        FL_IWDT_ReloadCounter(IWDT);
    }
}

void debug_cout_handle(void)
{
#if 0
    uint8_t i = 0;
    // SEGGER_RTT_TerminalPrintf (0,"tick:%d   ntc=%d\r\n",rt_tick_get(),Xadc);
    SEGGER_RTT_TerminalPrintf(0, "id:0x%X, tick[%7d]:", CAN_SEND_ID, rt_tick_get());

#if 1
    for (i = 0; i < ADC_CH_MAX; i++) {
        SEGGER_RTT_TerminalPrintf(0, " %7d", (uint32_t)(res_data_arr[i].Res_value));
    }

    SEGGER_RTT_TerminalPrintf(0, "  [TEMP:%d]", (int32_t)(TEMP_Value * 100)); // 温度
#endif
    // SEGGER_RTT_TerminalPrintf (0," %d  %d
    // %7d",res_data_arr[NTC_EN].Xadc,res_data_arr[NTC_EN].Xdac,(uint32_t)(res_data_arr[NTC_EN].Res_value/10));
    SEGGER_RTT_TerminalPrintf(0, "\n");
    // FL_GPIO_ToggleOutputPin(GPIOD,FL_GPIO_PIN_6);
#endif
}
